package org.ladybug.web.controller;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.ladybug.service.SearchService;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.util.Assert;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

public class SearchController extends AbstractController implements InitializingBean{

	private SearchService searchService;
	private String searchResultName = "searchResult";
	private String viewName;

	/**
	 * Contiene la mappatura tra nome parametro da passare al servizio di
	 * ricerca e nome del parametro da recuperare dalla request
	 */
	private Map<String, String> paramsMapper = new HashMap<String, String>();

	public void afterPropertiesSet() throws Exception {
		if(searchService==null){
			throw new Exception("Obbligatorio specificare la property searchService");
		}
		if(viewName==null){
			throw new Exception("Obbligatorio specificare la property viewName");
		}
		
	}
	
	
	@Override
	protected void initApplicationContext() throws BeansException {
		super.initApplicationContext();
	}

	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Iterator<String> keys = paramsMapper.keySet().iterator();
		Map<String, String> searchParams = new HashMap<String, String>();
		while (keys.hasNext()) {
			String key = keys.next();
			String reqParamName = paramsMapper.get(key);
			String paramValue = request.getParameter(reqParamName);
			if (paramValue != null && !paramValue.trim().equals("")) {
				searchParams.put(key, request.getParameter(reqParamName));
			}
		}
		Object result = searchService.find(null, searchParams);
		Map<String, Object> model = new HashMap<String, Object>();
		model.put(getSearchResultName(), result);
		return new ModelAndView(getViewName(), model);
	}

	

	

	public String getViewName() {
		return viewName;
	}

	/**
	 * Nome della vista da visualizzare
	 * @param viewName
	 */
	public void setViewName(String viewName) {
		this.viewName = viewName;
	}

	public Map getParamsMapper() {
		return paramsMapper;
	}

	/**
	 * Imposta la mappatura tra nome parametro usato dal servizio per eseguire
	 * la ricerca e nome del parametro in request da cui prelevare il valore
	 * 
	 * @param paramsMapper
	 */
	public void setParamsMapper(Map<String, String> paramsMapper) {
		this.paramsMapper = paramsMapper;
	}

	public SearchService getSearchService() {
		return searchService;
	}

	public void setSearchService(SearchService searchService) {
		this.searchService = searchService;
	}


	public String getSearchResultName() {
		return searchResultName;
	}


	public void setSearchResultName(String searchResultName) {
		this.searchResultName = searchResultName;
	}






}
